package com.kcsj.controller.basic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.kcsj.domain.user.Notice;
import com.kcsj.service.NoticeService;
import com.kcsj.util.JSONUtil;
import com.kcsj.util.MessageHelper;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

/**
 * @author 胡庆阳
 * 29th Nov. 2020
 */
@Slf4j
@WebServlet("/notice")
public class NoticeController extends HttpServlet {
    //获取最新通知
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取前端请求的信息
        String type = (String)request.getAttribute("roleName");
        Integer userId = Integer.parseInt(request.getAttribute("userId").toString());
        String total = request.getParameter("total");

        try {
            Notice notice = null;
            if(type.equals("销售")){
                notice = NoticeService.getInstance().findNewNotice(type,Integer.parseInt(total),null);
            }else {
               notice = NoticeService.getInstance().findNewNotice(type,Integer.parseInt(total),userId);
            }
            String json = JSON.toJSONString(notice);
            JSONObject data = MessageHelper.addData(json);
            //响应department_json到前端
            response.getWriter().println(data);
        }catch (SQLException e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
        }catch(Exception e){
            JSONObject message = MessageHelper.netWordError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }
    }

    //根据通知类型和用户类型查询通知
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //根据request对象，获得代表参数的JSON字串
        String json = JSONUtil.getJSON(request);
        try {
            JSONObject jsonObject = JSONObject.parseObject(json);
            String type = jsonObject.getString("type");
            Integer userId = jsonObject.getInteger("userId");
            //将JSON字串解析为student对象ser_json
            Collection<Notice> notices = null;
            if(type.equals("销售")){
                notices = NoticeService.getInstance().findAllByTypeAndUserId(type,null);
            }else {
                notices = NoticeService.getInstance().findAllByTypeAndUserId(type,userId);
            }

            String notices_json = JSON.toJSONString(notices, SerializerFeature.DisableCircularReferenceDetect);
            JSONObject data = MessageHelper.addData(notices_json);
            //响应departments_json到前端
            response.getWriter().println(data);
        }catch (SQLException e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
        }catch(Exception e){
            JSONObject message = MessageHelper.netWordError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }
    }
    //修改通知状态,修改为已读
    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json = JSONUtil.getJSON(request);
        try {
            JSONArray jsonArray = JSONObject.parseArray(json);
            List<Notice> notices = jsonArray.toJavaList(Notice.class);
            if (!notices.isEmpty()){
                NoticeService.getInstance().update(notices);
                JSONObject message = MessageHelper.addMessage("修改成功");
                response.getWriter().println(message);
            }else {
                JSONObject message = MessageHelper.addMessage("暂无新消息");
                response.getWriter().println(message);
            }
        } catch (SQLException e) {
            log.error(e.getMessage());
            e.printStackTrace();
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
        } catch (Exception e) {
            JSONObject message = MessageHelper.netWordError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }
    }
    //删除通知
    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        try {
            if (!id.equals("")) {
                //到数据库表中删除对应的学位
                NoticeService.getInstance().delete(Integer.parseInt(id));
                JSONObject message = MessageHelper.addMessage("删除成功");
                log.info("user{}删除成功",id);
                response.getWriter().println(message);
            }
        }catch (SQLException e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
        }catch(Exception e){
            JSONObject message = MessageHelper.netWordError();
            e.printStackTrace();
            response.getWriter().println(message);
        }
    }
}
